/**
 * 
 */
package info.niwota.dc;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;

/**
 * @author qiangli
 *
 */
public final class Preferences {
	//bg image
	public static final String BG_SELECT = "bg_select";
	
	//must match values compose_bg_select_values in arrays.xml
	public static final int BG_SELECT_NO_BACKGROUND = 0;
	public static final int BG_SELECT_PICK =1;
	public static final int BG_SELECT_GONE_WILD = 2;
	public static final int BG_SELECT_WALLPAPER = 3;
	public static final int BG_SELECT_SLIDES = 4;
	
	private static final String BG_PICK_PATH = "bg_pick_path";

	private static final String BG_URIS = "bg_uris";

	private static final String SEP = "\n";

	private static final String CLOCK_SCREEN_TIMEOUT = "clock.screen.timeout";

	private static final String CLOCK_WAKEUP_INTERVAL = "clock.wakeup.interval";

	public static final long SCREEN_TIMEOUT_DEFAULT = 5*1000; //seconds
	
	public static final int WAKEUP_INTERVAL_DEFAULT = 5; //ticks/minutes (one tick per min)
	
	private Preferences() {
	}

	public static String getBackgroundImagePath(Context context, String def) {
		try {
			final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
			return pref.getString(BG_PICK_PATH, def);
		} catch (Exception e) {
		}
		return def;
	}

	public static void putBackgroundImagePath(Context context, String path) {
		final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		pref.edit().putString(BG_PICK_PATH, path).commit();
	}
	public static void putBackgroundSelection(Context context, int sel) {
		final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		pref.edit().putInt(BG_SELECT, sel).commit();
	}
	
	public static int getBackgroundSelection(Context context) {
		final int def = BG_SELECT_NO_BACKGROUND;
		try {
			final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
			return pref.getInt(BG_SELECT, def);
		} catch (Exception e) {
		}
		return def;
	}
	
	public static void putUris(Context ctx, String[] uris) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		putUris(p, uris);
	}

	synchronized private static void putUris(SharedPreferences p, String[] uris) {
		Editor e = p.edit();
		if (uris == null || uris.length == 0) {
			e.putString(BG_URIS, "").commit();
		} else {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < uris.length; i++) {
				sb.append(uris[i]);
				sb.append(SEP);
			}
			e.putString(BG_URIS, sb.toString()).commit();
		}
	}
	
	public static String[] getUris(Context ctx) {
		final SharedPreferences p = PreferenceManager
		.getDefaultSharedPreferences(ctx);
		return getUris(p);
	}
	
	private static String[] getUris( SharedPreferences p) {
		try {
			String s = p.getString(BG_URIS, null);
			if (!TextUtils.isEmpty(s)) {
				return s.split(SEP);
			}
		} catch (Exception e) {
		}
		return null;
	}
	
	public static long getScreenTimeout(Context context) {
		long dv = SCREEN_TIMEOUT_DEFAULT;
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			String s = pref.getString(CLOCK_SCREEN_TIMEOUT, null);
			if (s != null) {
				long v = Long.parseLong(s);
				return v * 1000;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dv ;
	}
	
	public static int getWakeupInterval(Context context) {
		int defval = WAKEUP_INTERVAL_DEFAULT;
		try {
			final SharedPreferences pref = PreferenceManager
			.getDefaultSharedPreferences(context);
			String s = pref.getString(CLOCK_WAKEUP_INTERVAL, null);
			if (s == null) {
				return defval;
			}
			int v = Integer.parseInt(s);
			return ( v > 0 && v <= 60 ? v : defval);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defval;
	}
}
